$("#login-submit").click(function(e){
  e.preventDefault();
  var url = "/login?fundsource="+$("#fundsource").val()+"&username="+$("#username").val()+"&password="+$("#password").val();
  $.get(url, function(data) {
    window.location = "./home.html";
    /*$.mobile.changePage("./home.html#home", {
      type: "post",
      data: {balance:data.balance,fundingsource:data.fundsource}
    });*/
  });
});
$(document).bind("pagechange",function(e,data){
  if($.mobile.activePage.attr('id')=="home"){
    if(data.options.data){
      $("#funding-source").attr("alt",data.options.data.fundingsource);
      $("#funded-balance").html(data.options.data.balance.toFixed(2));
    }
    else{
      $.get("/service?function=getbalance&kind=Consumer", function(data) {
        $("#funding-source").attr("alt",data.fundsource);
        $("#funded-balance").html(data.balance.toFixed(2));
      });
    }
    $.get("/service?function=getschemebalance", function(data) {
      if(data.scheme != null){
        $("#virtual-currency-summary").show();
        $("#scheme-image").attr("alt",data.scheme);
        $("#scheme-name").html(data.scheme);
        $("#scheme-balance").html(data.schemebalance.toFixed(2));
        $.get("/service?function=gethistory&kind=Consumer", function(data) {
          
          console.log("Chris add transactions here!");
        });
      }
    });
  }
  if($.mobile.activePage.attr('id')=="fund"){
    $.get("/service?function=getbalance&kind=Consumer", function(data) {
      $("#slider-s").attr("max",data.balance);
    });
  }

  if($.mobile.activePage.attr('id')=="pay"){
    $("#pay-details-screen").hide();
    $.mobile.loading("hide");
    if(!$.getUrlVar("merchantId") || !$.getUrlVar("amount")){
      $.mobile.loading("show", {
        text: "Awaiting payment request",
        textVisible: true,
        theme: "c",
        html: ""
      });
    }
    else{
      $(".merchant-name").html($.getUrlVar("merchantId"));
      $(".merchant-amount").html($.getUrlVar("amount"));
      $("#slider-2").attr("max",$.getUrlVar("amount"));
      $("#slider-2").val($("#slider-2").attr("max")/10).slider("refresh");

      $(".total-amount").html(getPayTotal());
      $("#pay-details-screen").show();
      $("#slider-2").show();

      $("#slider-2").on("slidestop", function(event) {
        $(".total-amount").html(getPayTotal());
      });

      $("#flip-1").on("slidestop", function( event ) {
        if($("#flip-1").val()=="off"){
          $("#slider-container").hide();
        }
        else{
          $("#slider-container").show(); 
        }
        $(".total-amount").html(getPayTotal());
      });
    }
    
  }
    
});

function getPayTotal(){
  
  var total;
  if($("#flip-1").val()=="on"){
    total = parseInt($("#slider-2").val()) + parseInt($.getUrlVar("amount"));
  }
  else{
    total = parseInt($.getUrlVar("amount"));
  }
  return(total.toFixed(2));
  
}

$("#submit-currency").click(function(){
  $("#funding-amount").html(parseInt($("#slider-s").val()).toFixed(2));
  $("#funding-destination").html($("#destination").val());
});
$("#confirm-fund-button").click(function(e){
  e.preventDefault();
  
  var url = "/service?function=fundscheme&scheme="+$("#destination").val()+"&amount="+parseInt($("#slider-s").val()).toFixed(2);
  
  $.get(url, function(data) {
    $.mobile.changePage("#home");
  });
});
$("#confirm-pay-button").click(function(e){
  e.preventDefault();

  var url = "/service?function=pay&merchantId="+$.getUrlVar("merchantId")+"&amount="+getPayTotal()+"&scheme=serve";
  
  $.get(url, function(data) {
    $.mobile.changePage("#home");
  });
});



/* DOM loaded events... */
$(document).ready(function() {
  $('HTML').removeClass('no-js');
});
/* Window loaded events... */
$(window).load(function () {
  // JS to prevent framing in external sites
  if(top!=self){top.location=self.document.location;}
});
/* Function to test if element exists e.g. $("#anElement").exists() */
jQuery.fn.exists = function(){return jQuery(this).length>0;}
/* Function to get query string parameters */
$.extend({
  getUrlVars: function(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    return vars;
  },
  getUrlVar: function(name){
    return $.getUrlVars()[name];
  }
});